package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.Adopt;
import edu.school.entity.Goods;
import edu.school.entity.User;
import edu.school.service.AdoptService;
import edu.school.service.GoodsService;
import edu.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

@Controller
public class AdoptController {
    @Autowired
    UserService us;
    @Autowired
    AdoptService service;
    @Autowired
    GoodsService gs;
    @RequestMapping(value = "/adopt/toAdd")
    public String toAdd() {
        return "client/add_adopt";
    }
    @RequestMapping(value = "/adopt/add",method ={RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(Adopt record,HttpSession session,int id){
        User user = (User) session.getAttribute("user");
        if (null==user){
            return "<script>alert('请先登录');location.href='/toLogin.action';</script>";
        }
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreate_time(timeStamp);
        record.setGid(id);
        Goods goods = gs.findById(id);
        record.setUid(user.getId());
        record.setU_id(goods.getU_id());
        record.setState(String.valueOf(0));
        if (user.getId().equals(goods.getU_id())){
            return "<script>alert('不能自己领养自己的物品');history.go(-1);</script>";
        }
        if(service.save(record)){
            Goods goods1 = new Goods();
            goods1.setStatus(String.valueOf(1));
            goods1.setId(id);
            gs.update(goods1);
            return "<script>alert('认领成功');location.href='/client/adopt';</script>";

        }else{
            return "<script>alert('认领失败');history.go(-1);</script>";
        }

    }
    @RequestMapping(value = "/adopt/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(Adopt record,HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){

        PageInfo<Adopt> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);

        return "/adopt/list";
    }



    @RequestMapping("/adopt/delete")
    @ResponseBody
    public String ajaxdelete(Integer id) {
        boolean b =service.deleteAdopt(id);

        if(b){
            return "ok";
        }
        return "fail";

    }
    //批量删除
    @RequestMapping("/adopt/deletemany")
    @ResponseBody
    public String  deletemany(String ids) {
        System.err.println(ids);
        String[] arrayids=ids.split(",");//把数组里的值逗号隔开
        System.out.println("批量删除成功");

        service.deleteMany(arrayids);
        return "ok";//返回给ajax

    }
    @RequestMapping(value = "/adopt/agree",method ={RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String agree(Adopt record,Integer id) {
        record.setState("1");
        record.setId(id);
        if (service.update(record)) {
            return "<script>alert('审核成功');location.href='/adopt/list';</script>";
        } else {
            return "<script>alert('审核失败');history.go(-1);</script>";
        }
    }
    @RequestMapping(value = "/adopt/disagree",method ={RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String disagree(Adopt record,Integer id) {
        record.setState("2");
        record.setId(id);
        if (service.update(record)) {
            return "<script>alert('已拒绝');location.href='/adopt/list';</script>";
        } else {
            return "<script>alert('审核失败');history.go(-1);</script>";
        }
    }
}
